package com.jiuyou.storeController.mall.bbs;


import com.jiuyou.storeController.common.StoreBaseController;
import com.ruoyi.common.constant.DicConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.Member;
import com.ruoyi.common.core.domain.entity.StoreUserRole;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.shopping.domain.Bbs;
import com.ruoyi.shopping.domain.BbsLike;
import com.ruoyi.shopping.service.IBbsLikeService;
import com.ruoyi.shopping.service.IBbsService;
import com.ruoyi.store.domain.Store;
import com.ruoyi.store.service.IStoreService;
import com.ruoyi.technician.domain.StoreTechnician;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author chengliang
 * @date 2022/6/20
 **/
@RestController
@RequestMapping("/store/mall/bbs")
public class MallBBSStoreController extends StoreBaseController {
    @Autowired
    private IBbsService bbsService;
    @Autowired
    private IBbsLikeService bbsLikeService;
    @Autowired
    private IStoreService storeService;
    @PostMapping("/getList")
    public AjaxResult getList(String classify,Long minId,String myFlag ){

        //论坛查询
        Bbs bbsQuery = new Bbs();
        Long storeUserId=getLoginStoreUserId();

        List<Bbs> bbss =new ArrayList<Bbs>();
        TableDataInfo dataTable=null;
        if(StringUtils.isEmpty(myFlag)||"my".equals(myFlag)){
            //对外列表
            if(null!=classify){
                bbsQuery.setMessageClassify(classify);
            }
            if(null!=minId&&minId!=0){
                HashMap<String, Object> map = new HashMap<>();
                map.put("minId",minId);
                bbsQuery.setParams(map);
            }
            if("my".equals(myFlag)){
                bbsQuery.setPublishId(storeUserId);
            }
            bbsQuery.setBelongs(DicConstants.BUSINESS_Business.getCode());
            startPage();
            bbss= bbsService.selectBbsList(bbsQuery);
            dataTable = getDataTable(bbss);
        }else{

            //我发布的或收藏的一些
//            if("my".equals(myFlag)){
//              //我发布的,
//                bbs.setPublishId(loginMemberId);
//            }else{//(DicConstants.BBS_OPTIOPN_COLLECT.getCode().equals(myFlag)||DicConstants.BBS_OPTIOPN_LIKE.getCode().equals(myFlag)){
                //收藏 或者点赞
//                bbs.getParams().put("clCreateBy",loginMemberId);
//                bbs.getParams().put("clBelongType",myFlag);
                BbsLike bbsLikeQuery=new BbsLike();
                bbsLikeQuery.setCreateBy(storeUserId);
                bbsLikeQuery.setBelongType(myFlag);
                bbsLikeQuery.setBelongs(DicConstants.BUSINESS_Business.getCode());
                startPage();
                List<BbsLike>  bbsLikeList= bbsLikeService.selectBbsLikeList(bbsLikeQuery);
                dataTable = getDataTable(bbsLikeList);
                if(bbsLikeList.size()>0){
                    List<Long> bbsIds=bbsLikeList.stream().map(BbsLike::getForeignId).collect(Collectors.toList());
                    bbsQuery.getParams().put("bbsIds",bbsIds);
                    bbsQuery.getParams().put("unSort","Y");
                    List<Bbs>  bbssTemp= bbsService.selectBbsList(bbsQuery);
                  Map<Long,Bbs> mapBbs=bbssTemp.stream().collect(Collectors.toMap(Bbs::getId, Function.identity()));
                    for (Long bbsId : bbsIds) {
                        if(mapBbs.containsKey(bbsId)){
                            bbss.add(mapBbs.get(bbsId));
                        }

                    }
                }

                dataTable.setRows(bbss);
//            }
        }




//        ArrayList<JSONObject> result = new ArrayList<>();
//        bbss.forEach(e->{
//            JSONObject jso = new JSONObject();
//            //查询点赞数
//            jso.put("headImg",e.getPublishHead());
//            jso.put("nickName",e.getPublishName());
//            jso.put("title",e.getHeadline());
//            jso.put("classify", DictUtils.getDictValue("bbs_message_classify",e.getMessageClassify()));
//            jso.put("context",e.getMainBody());
//            jso.put("video",e.getVideoList());
//            jso.put("imgs",e.getImageList());
//            jso.put("headImg",e.getPublishHead());
//
//            result.add(jso);
//        });
        return AjaxResult.success(dataTable);
    }
    @PostMapping("/getSearch")
    public AjaxResult getSearch(String searchValue){
        startPage();
        //论坛查询
        Bbs bbs = new Bbs();
        bbs.setHeadline(searchValue);
        bbs.setBelongs(DicConstants.BUSINESS_Business.getCode());
        List<Bbs> bbss = bbsService.selectBbsList(bbs);
        TableDataInfo dataTable = getDataTable(bbss);

//        ArrayList<JSONObject> result = new ArrayList<>();
//        bbss.forEach(e->{
//            JSONObject jso = new JSONObject();
//            //查询点赞数
//            jso.put("headImg",e.getPublishHead());
//            jso.put("nickName",e.getPublishName());
//            jso.put("title",e.getHeadline());
//            jso.put("classify", DictUtils.getDictValue("bbs_message_classify",e.getMessageClassify()));
//            jso.put("context",e.getMainBody());
//            jso.put("video",e.getVideoList());
//            jso.put("imgs",e.getImageList());
//            jso.put("headImg",e.getPublishHead());
//
//            result.add(jso);
//        });
        return AjaxResult.success(dataTable);
    }
    @PostMapping("/getDetail/{id}")
    public AjaxResult getDetail(@PathVariable Long id){
        Bbs bbs = bbsService.selectBbsById(id);
        return AjaxResult.success(bbs);
    }
    @PostMapping("/del/{id}")
    public AjaxResult del(@PathVariable Long id){
        Long storeUserId=getLoginStoreUserId();
        Bbs bbsOld=   bbsService.selectBbsById(id);
        if(bbsOld.getPublishId()!=storeUserId){
//                不是自己的数据
            return AjaxResult.unAuthorize();
        }
        Long ids[]=new Long[]{id};
       bbsService.logicDeleteBbs(ids,DicConstants.BUSINESS_Business.getCode(),storeUserId);
        return AjaxResult.success();
    }


    @PostMapping("/publish")
    public AjaxResult publish(Bbs bbs){


        Long storeUserId=getLoginStoreUserId();
        StoreTechnician storeTechnician= getStoreTechnician();
        String publishHead="";
        String publishName="";
        StoreUserRole storeUserRole;
        if(null!=storeTechnician){
            //技师
            storeUserRole= getStoreUserRole(DicConstants.STORE_Identity_JiShi.getCode());
            publishHead=storeTechnician.getTechImage();
            publishName=storeTechnician.getTechName();
        }else{
            storeUserRole=getDefultStoreUserRole(DicConstants.STORE_Identity_YunYing.getCode(),DicConstants.STORE_Identity_Manager.getCode(),DicConstants.STORE_Identity_KeFu.getCode());
            if(null==storeUserRole){
                return AjaxResult.error("该账号无法操作");
            }
            //取门店信息
            Long storeId= storeUserRole.getStoreId();
            Store store=storeService.selectStoreById(storeId);
            publishHead=store.getStoreImage();
            publishName=store.getStoreName();
        }
        bbs.setPublishId(storeUserId);
        bbs.setPublishName(publishName);
        bbs.setPublishHead(publishHead);
        bbs.setStoreId(storeUserRole.getStoreId());
        bbs.setStoreUserRoleId(storeUserRole.getId());
        bbs.setRoleType(storeUserRole.getRoleType());
        bbs.setBelongs(DicConstants.BUSINESS_Business.getCode());
        Bbs bbsUpdate=new Bbs();

        bbsUpdate.setPublishId(storeUserId);
        bbsUpdate.setPublishName(publishName);
        bbsUpdate.setPublishHead(publishHead);
        bbsUpdate.setBelongs(DicConstants.BUSINESS_Business.getCode());
        bbsUpdate.setMessageClassify(bbs.getMessageClassify());
        bbsUpdate.setHeadline(bbs.getHeadline());
        bbsUpdate.setMainBody(bbs.getMainBody());
        bbsUpdate.setVideoList(bbs.getVideoList());
        bbsUpdate.setImageList(bbs.getImageList());

        if(bbs.getId()!=null){
            //修改
            Bbs bbsOld=   bbsService.selectBbsById(bbs.getId());
            if(bbsOld.getPublishId()!=storeUserId){
//                不是自己的数据
                return AjaxResult.unAuthorize();
            }

            bbsUpdate.setId(bbs.getId());
            bbsService.updateBbs(bbsUpdate);
        }else{
            bbsService.insertBbs(bbsUpdate);
        }

        return AjaxResult.success(bbsUpdate);
    }

}
